package cn.kgc.javaweb.service.impl;


import cn.kgc.javaweb.dao.UnitDao;
import cn.kgc.javaweb.dao.impl.UnitDaoImpl;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.Unit;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.service.UnitService;
import cn.kgc.javaweb.util.ResponseResult;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class UnitServiceImpl implements UnitService {

    private UnitDao unitDao = new UnitDaoImpl();

    @Override//用、用
    public ResponseResult selectPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<Unit> units =  unitDao.selectPage(userPageDTO);
            // 查询数据总条数
            Long  total =  unitDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(units);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用
    public ResponseResult selectById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            Unit unit =  unitDao.selectById(id);
            responseResult.setData(unit);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用
    public ResponseResult update(Unit uni,Integer id) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            unitDao.update(uni,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用、用
    public ResponseResult add(Unit uni) {

        ResponseResult responseResult = ResponseResult.builder().build();
            try {
                unitDao.add(uni);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }

        return responseResult;
    }

    @Override//用、用
    public ResponseResult delete(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            unitDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }
}
